package top.jsjkxyjs.blackbox.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.jsjkxyjs.blackbox.entity.*;
import top.jsjkxyjs.blackbox.service.*;
import top.jsjkxyjs.blackbox.utils.Msg;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 19553
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	UserService userService;
	@Autowired
	CarService carService;
	@Autowired
	IotService iotService;
	@Autowired
	CarTypeService carTypeService;
	@Autowired
	DevBindService devBindService;
	@Autowired
	AddressService addressService;
	@Autowired
	EventService eventService;

	@RequestMapping("/setting")
	public String setting(Model model, HttpSession httpSession) {
		User user = (User) httpSession.getAttribute("user");
		model.addAttribute("user", user);
		if (user.getRole() == 1) {
			return "pages/user/userinfo";
		} else {
			return "pages/service/userinfo";
		}
	}

	@ResponseBody
	@PostMapping("/checkphone")
	public Msg checkPhone(String phone) {
		boolean foundUser = userService.foundUser(phone);
		return foundUser ? Msg.tips("您已经注册过了，请登陆！", 400) : Msg.success();
	}

	@RequestMapping("/bindcenter")
	public String bindCartoUser(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		List<Cartype> allCarType = carTypeService.getAllCarType();
		List<Car> userAllCars = carService.getUserAllCars(user);
		if (userAllCars.size() == 0) {
			userAllCars.add(new Car(-1, "请添加车辆后再进行操作！", 0, 0));
		}
		model.addAttribute("allCarType", allCarType);
		model.addAttribute("userAllCars", userAllCars);
		return "pages/user/bindcenter";
	}

	@PostMapping("/bindcar")
	@ResponseBody
	public Msg addCarToUser(Car car, HttpSession httpSession) {
		if (car.getLicensePlate().length() == 7) {
			User user = (User) httpSession.getAttribute("user");
			if (carService.checkLicensePlate(user, car)) {
				if (userService.bindCartoUser(user, car)) {
					return Msg.success();
				} else {
					return Msg.fail();
				}
			} else {
				return Msg.tips("此车牌号已经录入系统，禁止再次录入！", 400);
			}
		} else {
			return Msg.tips("车牌号长度不正确！", 400);
		}
	}

	@PostMapping("/bindiot")
	@ResponseBody
	public Msg addIotToUser(Devbind devbind, HttpSession session) {
		if ((devbind.getDevId() + "").length() == 9) {
			User user = (User) session.getAttribute("user");
			//检测设备是否在线，设备在线的话才允许插入
			if (iotService.getIotDevCase(devbind.getDevId())) {
				if (devBindService.addIotToUser(devbind, user)) {
					return Msg.success();
				} else {
					return Msg.tips("绑定失败", 400);
				}
			}
			return Msg.tips("请先将设备连接至网络激活后绑定或已经被绑定！", 400);
		}

		return Msg.tips("设备编号不正确请检查后重试", 400);
	}

	@RequestMapping("/cars")
	public String cars() {
		return "pages/user/cars";
	}

	@RequestMapping("/iots")
	@ResponseBody
	public List getUsersAllIots(HttpSession session) {
		User user = (User) session.getAttribute("user");
		return devBindService.getUsersAllIots(user.getId());
	}

	@RequestMapping("/iot/{devId}")
	public String iotDataShow(@PathVariable("devId") int devId) {
		return "pages/user/iotdatashow";
	}

	@RequestMapping("/getcity")
	@ResponseBody
	public List getCity(Integer province) {
		List<Address> cityList = addressService.findByParent(province);
		return cityList;
	}

	@RequestMapping("/getregion")
	@ResponseBody
	public List getRegion(int city) {
		List<Address> regionList = addressService.findByParent(city);
		return regionList;
	}

	@PostMapping("/infochange")
	@ResponseBody
	public Msg infoChange(User user, HttpSession session) {
		user.setId(((User) session.getAttribute("user")).getId());
		if (userService.changeUserInfo(user)) {
			return Msg.success();
		} else {
			return Msg.tips("更新用户数据失败！", 400);
		}
	}

	@RequestMapping("/event")
	public String event(HttpSession session, Model model) {
		User user = (User) session.getAttribute("user");
		if (user.getRole() == 1) {
			return "pages/user/index";
		}
		List<Event> allEvents = eventService.getAllEvents();
		model.addAttribute("events", allEvents);
		return "pages/service/event";
	}

	@RequestMapping("/eventend")
	@ResponseBody
	public Msg enenteEnd(HttpSession session, int id) {
		User user = (User) session.getAttribute("user");
		if (eventService.eventEnd(user, id)) {
			return Msg.success();
		} else {
			return Msg.tips("事件处理失败", 400);
		}
	}
}
